package com.oraycn.esbasic.objectManagement.managers;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; 

public class ObjectManager<TPKey, TObject> {
    private HashMap<TPKey, TObject> objectDictionary = new HashMap<TPKey, TObject>();

    public int size()
    {
        return this.objectDictionary.size();
    }

    /**
     * 向Map中添加键值对，若有相同的key值则先删除，后加入（相当于新值覆盖旧值）
     */
    public void add(TPKey key, TObject obj)
    {
        if (this.objectDictionary.containsKey(key))
        {
            this.objectDictionary.remove(key);
        }

        this.objectDictionary.put(key, obj);
    }

    public void remove(TPKey id)
    {
        if (this.objectDictionary.containsKey(id))
        {
            this.objectDictionary.remove(id);
        }
    }

    public void clear()
    {
        this.objectDictionary.clear();
    }

    public TObject get(TPKey id)
    {
        if (this.objectDictionary.containsKey(id))
        {
            return this.objectDictionary.get(id);
        }
        return null;
    }

    public boolean containsKey(TPKey id)
    {
        return this.objectDictionary.containsKey(id);
    }

    public List<TObject> getAll()
    {
        return new ArrayList<TObject>( this.objectDictionary.values()) ;
    }

    public List<TPKey> getKeyList()
    {
        return new ArrayList<TPKey>( this.objectDictionary.keySet()) ;
    }

    public HashMap<TPKey,TObject> ToHashMap()
    {
        return this.objectDictionary;
    }
}
